{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#每次打开后，先执行这里！！！！！\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#生成一个array的方法\n",
    "\n",
    "#创建一个list，利用list赋值\n",
    "alist=[1,2,3]\n",
    "arr=np.array(alist)\n",
    "\n",
    "#创建一个全零向量\n",
    "arr1=np.zeros(5)\n",
    "\n",
    "#创建一个从0-9的向量\n",
    "arr2=np.arange(10)\n",
    "\n",
    "#或者5-14的向量,注意！！！end\n",
    "arr3=np.arange(5,15)\n",
    "\n",
    "#从0到10，均分5份\n",
    "arr4=np.linspace(0,10,5)\n",
    "\n",
    "#创建5X5 array of zeros\n",
    "image=np.zeros((5,5))\n",
    "image\n",
    "\n",
    "#创建5X5X5的cube of 1’s,the astype()method 设置元素的数据类型\n",
    "cube=np.zeros((5,5,5)).astype(int)+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#在生成一个arrays时，如果没有明确说明类型，是按照操作系统的特点来确定，比如64-bit的系统，生成的精度为64位\n",
    "#一般来说，没有必要，这需要耗费很多的内存，所以可以根据需求，设置不同的数据类型，方法如下：\n",
    "arr5=np.zeros((5,5),dtype=int)\n",
    "#print arr5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.  0.  0. ...,  0.  0.  0.]\n"
     ]
    }
   ],
   "source": [
    "#对于已经存在的array可以通过reshape重新构建\n",
    "arr6=np.arange(1000)\n",
    "#下面两种方法产生一样的效果\n",
    "arr3D=arr6.reshape((10,10,10))\n",
    "arr3D_1=np.reshape(arr6,(10,10,10))\n",
    "#将多维数据扁平化\n",
    "arr4D=np.zeros((10,10,10,10))\n",
    "arr1D=arr4D.ravel()\n",
    "print arr1D\n",
    "arr6=arr6+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 2.0, 'Hello') (2, 3.0, 'World')]\n"
     ]
    }
   ],
   "source": [
    "#在一个array中，每一列可以是不同的数据类型，通过如下方法构建\n",
    "recarry=np.zeros((2,),dtype=('i4,f4,a10'))#i4表示32位整数，f4表示32位float，a10表示10个字符长度string\n",
    "toadd = [(1,2.,'Hello'),(2,3.,\"World\")]\n",
    "recarry[:]=toadd\n",
    "#recarry=toadd\n",
    "print recarry\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(0, 0.0, 'hello') (0, 0.0, 'hello')]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array(['hello', 'hello'], \n",
       "      dtype='|S10')"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#细化到每列构建，赋值到recarry\n",
    "recarr=np.zeros((2,),dtype=('i4,f4,a10'))\n",
    "col1=np.arange(1,dtype=int)\n",
    "col2=np.zeros(2,dtype=np.float32)\n",
    "col3=['hello','abc']\n",
    "\n",
    "col=zip(col1,col2,col3)#这里利用了zip函数\n",
    "\n",
    "recarr[:]=col\n",
    "print recarr\n",
    "\n",
    "#给每列取名字\n",
    "recarr.dtype.names=('intergers','float','string')\n",
    "recarr['string']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(array([8], dtype=int64),)\n",
      "[False False False False False False  True  True  True]\n"
     ]
    }
   ],
   "source": [
    "#索引和切片的方法\n",
    "arr7=np.arange(9)\n",
    "index=np.where(arr7>5)\n",
    "print index\n",
    "\n",
    "index2=arr7>5\n",
    "print index2\n",
    "#同样是获得索引信息，但使用bool型要比where快的多，通过某些转换就能获得准确的index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "img1=np.zeros((20,20))+3\n",
    "img1[-4:4,-4:4]=6\n",
    "img1[-7:7,-7:7]=9\n",
    "\n",
    "index1 = img1 > 2\n",
    "index2 = img1 < 6\n",
    "compound_index = index1 & index2\n",
    "\n",
    "compound_index = (img1 > 3) & (img1 < 7)\n",
    "img2 = np.copy(img1)\n",
    "img2[compound_index] = 0\n",
    "#print img2\n",
    "index3 = img1 == 9\n",
    "index4 = (index1 & index2) | index3\n",
    "img3 = np.copy(img1)\n",
    "img3[index4] = 0\n",
    "\n",
    "#print img3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
